﻿@inherits LayoutComponentBase
@using Covenant.Core
@using Covenant.Models.Covenant
@using Covenant.Components.Events
@using Covenant.Components.CovenantUsers
@using Microsoft.AspNetCore.Identity
@using Microsoft.AspNetCore.Components.Routing
@using Microsoft.AspNetCore.Components.Authorization
@inject UserManager<CovenantUser> UserManager
@inject SignInManager<CovenantUser> SignInManager
@inject AuthenticationStateProvider AuthenticationStateProvider
@inject NavigationManager NavigationManager
@inject ICovenantService ICovenantService

<AuthorizeView>
    <NotAuthorized>
        <RedirectToLogin />
    </NotAuthorized>
    <Authorized>
        @if (UserManager.Users.Count() <= 1)
        {
            <RedirectToLogin />
        }
        else
        {
            <nav class="navbar navbar-expand navbar-dark sticky-top bg-dark">
                <a class="navbar-brand ml-3 align-items-center" href="/">
                    <img src="/images/covenant-white.png" alt="Covenant" height="28" width="101">
                </a>
                <ul class="navbar-nav ml-auto">
                    @{
                        CovenantUser covuser = GetUser();
                        @if (covuser != null)
                        {
                            <li class="nav-item text-nowrap">
                                <a class="nav-link" href="/covenantuser/edit/@covuser.Id">Welcome, @covuser.UserName!</a>
                            </li>
                            <li class="nav-item text-nowrap">
                                <a class="nav-link" href="/covenantuser/logout">Logout</a>
                            </li>
                        }
                    }
                </ul>
            </nav>
            <EventNotifier />
            <div id="covenant-container" class="container-fluid">
                <div class="d-flex flex-row">
                    <nav class="col-sm-2 bg-light sidebar">
                        <div class="sidebar-sticky">
                            <ul class="nav flex-column">
                                <li class="nav-item">
                                    <NavLink href="/home" id="nav-dashboard" class="nav-link" Match="NavLinkMatch.Prefix">
                                        <span class="fe fe-home"></span>
                                        Dashboard
                                    </NavLink>
                                </li>
                                <li class="nav-item">
                                    <NavLink href="/listener" id="nav-listeners" class="nav-link" Match="NavLinkMatch.Prefix">
                                        <span class="fe fe-headphones"></span>
                                        Listeners
                                    </NavLink>
                                </li>
                                <li class="nav-item">
                                    <NavLink href="/launcher" id="nav-launchers" class="nav-link" Match="NavLinkMatch.Prefix">
                                        <span class="fe fe-zap"></span>
                                        Launchers
                                    </NavLink>
                                </li>
                                <li class="nav-item">
                                    <NavLink href="/grunt" id="nav-grunts" class="nav-link" Match="NavLinkMatch.Prefix">
                                        <span class="fe fe-terminal"></span>
                                        Grunts
                                    </NavLink>
                                </li>
                                <li class="nav-item">
                                    <NavLink href="/template" id="nav-templates" class="nav-link" Match="NavLinkMatch.Prefix">
                                        <span class="fe fe-code"></span>
                                        Templates
                                    </NavLink>
                                </li>
                                <li class="nav-item">
                                    <NavLink href="/grunttask" id="nav-tasks" class="nav-link" Match="NavLinkMatch.Prefix">
                                        <span class="fe fe-package"></span>
                                        Tasks
                                    </NavLink>
                                </li>
                                <li class="nav-item">
                                    <NavLink href="/grunttasking" id="nav-taskings" class="nav-link" Match="NavLinkMatch.Prefix">
                                        <span class="fe fe-layers"></span>
                                        Taskings
                                    </NavLink>
                                </li>
                                <li class="nav-item">
                                    <NavLink href="/graph" id="nav-graph" class="nav-link" Match="NavLinkMatch.Prefix">
                                        <span class="fe fe-share-2"></span>
                                        Graph
                                    </NavLink>
                                </li>
                                <li class="nav-item">
                                    <NavLink href="/data" id="nav-data" class="nav-link" Match="NavLinkMatch.Prefix">
                                        <span class="fe fe-database"></span>
                                        Data
                                    </NavLink>
                                </li>
                                <li class="nav-item">
                                    <NavLink href="/covenantuser" id="nav-users" class="nav-link" Match="NavLinkMatch.Prefix">
                                        <span class="fe fe-users"></span>
                                        Users
                                    </NavLink>
                                </li>
                            </ul>
                        </div>
                    </nav>

                    <main role="main" class="col-sm-10 pt-3">
                        @Body
                    </main>
                </div>
                <footer>
                    <p></p>
                </footer>
            </div>
        }
    </Authorized>
</AuthorizeView>

@code {
    private CovenantUser GetUser()
    {
        Task<AuthenticationState> t1 = AuthenticationStateProvider.GetAuthenticationStateAsync();
        t1.Wait();
        Task<CovenantUser> t2 = UserManager.GetUserAsync(t1.Result.User);
        t2.Wait();
        return t2.Result;
    }
}